const utils = require('./utils');
const _ = require('lodash');

//使用模拟点击的方式获取缓存图片
(async () => {
    const browser = await utils.connectBrowser('127.0.0.1', 9222);
    const page = await browser.newPage();
    await page.setViewport({width: 1280, height: 800});
    await page.goto("http://pic.netbian.com/4kfengjing/");

    const imgUrls = await page.$$eval('ul.clearfix li a', pics => {
        let picArray = Array.prototype.slice.call(pics);
        return picArray.map(e => e.href);
    });

    await utils.sleep(2000);
    const bar = utils.progressBar(imgUrls.length);

    for (const i of _.range(0, imgUrls.length)) {
        bar(imgUrls[i]);

        await page.goto(imgUrls[i], {
            waitUntil: "load",
            timeout: 10000
        });

        const picUrl = await page.$eval('div.photo-pic img', e => e.src);
        await utils.downloadCachedResource(page, picUrl, utils.buildFilePath(picUrl, './download'));
        await utils.sleep(2000);
    }

    await utils.sleep(2000);
    await page.close();
    await browser.disconnect();
})();